package control.ioio.quadricopter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Environment;


public class DataRegister {
     
    private FileOutputStream fos =null;
    private File file;
	private Boolean first_data = true;
	
	public DataRegister(String filename){
		file = new File(Environment.getExternalStorageDirectory(), filename);
		byte[] data = new String("data=[").getBytes();
		try {
		    fos = new FileOutputStream(file);
		    fos.write(data);
		    fos.flush();
		} catch (FileNotFoundException e) {
		    // handle exception
		} catch (IOException e) {
		    // handle exception
		}		
	}
	
	public void closeFile(){
		byte[] data = new String("];").getBytes();
		try {
		    fos.write(data);
		    fos.flush();
		    fos.close();
		    fos = null;
		} catch (FileNotFoundException e) {
		    // handle exception
		} catch (IOException e) {
		    // handle exception
		}
	}
	
	public void updateFile(float time, float[] orientation, float[] orientation2){
		byte[] data;
		if(first_data){
			data = new String(time+ ","+Float.toString(orientation[0])+","+Float.toString(orientation[1])+","+Float.toString(orientation[2])+ ","+Float.toString(orientation2[0])+","+Float.toString(orientation2[1])+","+Float.toString(orientation2[2])).getBytes();
			first_data = false;
		}
		else{
			data = new String("; \n" + time+ ","+Float.toString(orientation[0])+","+Float.toString(orientation[1])+","+Float.toString(orientation[2])+ ","+Float.toString(orientation2[0])+","+Float.toString(orientation2[1])+","+Float.toString(orientation2[2])).getBytes();
		}
		
		try {
		    fos.write(data);
		    fos.flush();
		} catch (FileNotFoundException e) {
		    // handle exception
		} catch (IOException e) {
		    // handle exception
		}
	}
	
	
	
}

